
use crate::ui::{LWindowInfo, PageUI, RouteInfo};
use eframe::epaint::Color32;
use egui::{Button, CentralPanel, FontId, RichText, SidePanel,  Ui, vec2};
use crate::ui::ui_chat_msg::UIChatMsg;

pub(crate) struct HomeUI {
    selection_poition: u8,
    page_ui_vec: Vec<Box<dyn PageUI>>,
}

const LIST_SIDE_NAMES: [&str; 1] = [
    "消息聊天室",
];

impl HomeUI {
    pub(crate) fn new() -> Self {
        let ui_vec: Vec<Box<dyn PageUI>> = vec![
            Box::new(UIChatMsg::new()),
        ];
        Self {
            selection_poition: 0,
            page_ui_vec: ui_vec,
        }
    }

    pub(crate) fn show_ui(&mut self, route_page: &mut RouteInfo, ui: &mut Ui) {
        let rect = ui.max_rect();
        let f_width = ui.available_width();
        let f_heigth = ui.available_height();
        let winfo = LWindowInfo {
            f_width,
            f_heigth
        };
        ui.painter()
            .rect_filled(rect, 0.0, Color32::from_rgb(250, 250, 250));

        SidePanel::left("left_side").show(ui.ctx(), |ui| {
            egui::scroll_area::ScrollArea::vertical().show(ui, |ui| {
                egui::Grid::new("lll").num_columns(1).show(ui,|ui|{
                    for i in 0..LIST_SIDE_NAMES.len() {
                        let rich_text = RichText::new(LIST_SIDE_NAMES[i])
                            .font(FontId::proportional(14.0))
                            .color(Color32::BLACK);

                        let button = Button::new(rich_text);
                        if ui.add_sized(vec2(120.0, 46.0), button).clicked() {
                            self.selection_poition = i as u8;
                        }
                        ui.end_row();
                    }
                });

            });
        });

        CentralPanel::default().show(ui.ctx(), |ui| {
            ui.with_layout(egui::Layout::top_down(egui::Align::Center), |ui| {
                if let Some(page_ui_box) = self.page_ui_vec.get_mut(self.selection_poition as usize)
                {
                    page_ui_box.show_ui(ui, winfo);
                }
            });
        });
    }
}
